<?php
namespace app\admin\controller\admin;
class Pwd extends \app\admin\controller\admin\BaseAdmin{ 
    public function index(){
        //把用户信息传递给模板
        $data = [];
        return view('admin/pwd',$data);
    }
    public function save(){
        $request = \think\Request::instance();
        $request_data['old_pwd'] = $request->post('old_pwd');
        $request_data['r_pwd_one'] = $request->post('r_pwd_one');
        $request_data['r_pwd_two'] = $request->post('r_pwd_two');
        //验证输入的内容是否为空
        $rule = [
            'old_pwd|旧密码'  => 'require',
            'r_pwd_one|新密码'   => 'require|min:6',
            'r_pwd_two|确认密码' => 'require|confirm:r_pwd_one',
        ];
        $validate = new \think\Validate($rule);
        if (!$validate->check($request_data)) {
            return $this->error($validate->getError());
        }
        //验证输入的旧密码是否和数据库中的密码一致
        $password = $this->adminData->password;
        $is = password_verify($request_data['old_pwd'],$password);
        if(!$is){
            return $this->error('旧密码错误，请核对密码！');
        }
        //修改密码
        $data = [];
        $data['password'] = password_hash($request_data['r_pwd_one'],PASSWORD_DEFAULT);
        //将新密码插入数据库
        $this->adminData->allowField(true)->save($data);
        return $this->success('修改密码成功！下次登录请使用新密码！','admin/Login/index');        
    }
}
